#include "../include/Terrain.h"
#include "../include/MainWindow.h"
#include "../include/SkyBox.h"
#include "../include/Functions.h"
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>

void updateKeys(bool *keyDown, MainWindow *app);

int main(int argc, char **argv) {
    bool keyDown[6] = {false, //W
                       false, //A
                       false, //S
                       false, //D
                       false, //Z
                       false  //X
                      };
    sf::Clock clock;
    MainWindow app(800, 600, "Assignment 04");
    app.setClock(&clock);
    app.loadingScreen("Loading shaders...");
    app.setupShaders();
    app.loadingScreen("Loading terrain...");
	app.setTerrain("resources/terrain/heightmap.jpg");
	app.loadingScreen("Loading water...");
	app.setWater("resources/images/water256.jpg");
	app.loadingScreen("Loading skybox...");
    app.setSkybox("resources/images/stormydays_large.jpg");
    //app.setSkybox("resources/images/violentdays_large.jpg");
    app.loadingScreen("Loading models...");
    app.loadModels();
    while(app.isOpen()) {
        static bool framesOn = true;
        //Process events
        sf::Event event;
        while(app.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                app.close();
            } else if(event.type == sf::Event::KeyPressed) {
                switch(event.key.code) {
                case sf::Keyboard::W:
                    keyDown[0] = true;
                    break;
                case sf::Keyboard::A:
                    keyDown[1] = true;
                    break;
                case sf::Keyboard::S:
                    keyDown[2] = true;
                    break;
                case sf::Keyboard::D:
                    keyDown[3] = true;
                    break;
                case sf::Keyboard::Escape:
                    app.close();
                    break;
                case sf::Keyboard::F1: //Toggle wireframe
                    static bool isWireframe = false;
                    isWireframe = !isWireframe;
                    if(isWireframe)
                        glPolygonMode(GL_FRONT, GL_LINE);
                    else
                        glPolygonMode(GL_FRONT, GL_FILL);
                    break;
                case sf::Keyboard::F2: //Toggle frame counter
                    framesOn = !framesOn;
                    break;
                case sf::Keyboard::F3: //Toggle fullscreen
                    static bool isFullscreen = false;
                    isFullscreen = !isFullscreen;
                    if(isFullscreen) {
						app.create(sf::VideoMode::getFullscreenModes().at(0), "Assignment 4", sf::Style::Fullscreen, sf::ContextSettings(24, 0, 0, 2, 0));
                    } else {
						app.create(sf::VideoMode(800, 600),"Assignment 4", sf::Style::Default, sf::ContextSettings(24, 0, 0, 2, 0));
                    }
                    break;
                case sf::Keyboard::Z:
                    keyDown[4] = true;
                    break;
                case sf::Keyboard::X:
                    keyDown[5] = true;
                    break;
                case sf::Keyboard::E:
                    app.keyHandler(sf::Keyboard::E);
                    break;
                default:
                    break;
                }
            } else if(event.type == sf::Event::KeyReleased) {
                switch(event.key.code) {
                case sf::Keyboard::W:
                    keyDown[0] = false;
                    break;
                case sf::Keyboard::A:
                    keyDown[1] = false;
                    break;
                case sf::Keyboard::S:
                    keyDown[2] = false;
                    break;
                case sf::Keyboard::D:
                    keyDown[3] = false;
                    break;
                case sf::Keyboard::Escape:
                    app.close();
                    break;
                case sf::Keyboard::Z:
                    keyDown[4] = false;
                    break;
                case sf::Keyboard::X:
                    keyDown[5] = false;
                    break;
                default:
                    break;
                }
            } else if(event.type == sf::Event::MouseMoved) {
                int xDiff = event.mouseMove.x-(app.getSize().x/2);
                int yDiff = event.mouseMove.y-(app.getSize().y/2);
                app.adjustRotation(((float)xDiff/50)*10, ((float)yDiff/50)*10);
            }
        }
        if(clock.getElapsedTime().asMilliseconds() >= (1000.0/200)) {
            sf::Mouse::setPosition(sf::Vector2i(app.getSize().x/2, app.getSize().y/2), app);
            updateKeys(keyDown, &app);
            app.render();
            if(framesOn == true) {
                app.printFPS();
            }
            clock.restart();
        }
        app.display();
    }
}

void updateKeys(bool *keyDown, MainWindow *app) {
    if(keyDown[0]) { //W
        app->keyHandler(sf::Keyboard::W);
    }
    if(keyDown[1]) { //A
        app->keyHandler(sf::Keyboard::A);
    }
    if(keyDown[2]) { //S
        app->keyHandler(sf::Keyboard::S);
    }
    if(keyDown[3]) { //D
        app->keyHandler(sf::Keyboard::D);
    }
    if(keyDown[4]) { //Z
        app->keyHandler(sf::Keyboard::Z);
    }
    if(keyDown[5]) { //X
        app->keyHandler(sf::Keyboard::X);
    }
}
